package excel.util;

import excel.pojo.User;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 秉性哥
 * @createTime 2022/10/28 14:13
 * @description 导入工具类
 */
public class ImportUtils {
    public static void importExcel(String fileName,String pathFile,ArrayList<User> userList){
        String allPath = pathFile+"\\"+fileName;
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(allPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Sheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0;i<=lastRowNum;i++){
            Row row = sheet.getRow(i);
            if (row!=null){
                List<String>list = new ArrayList<>();
                for (Cell cell:row){
                    cell.setCellType(CellType.STRING);
                    String value = cell.getStringCellValue();
                    list.add(value);
                }
                if (list.size()>0){
                    User user = new User();
                    user.setUsername(list.get(0));
                    user.setPassword(list.get(1));
                    user.setEnd_login_date(list.get(2));
                    user.setFail_count(Integer.valueOf(list.get(3)));
                    userList.add(user);
                }
            }
        }
    }
}
